﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XRPGLibrary.TileEngine
{
    public class TileMap
    {
        #region Field Region
        List<Tileset> tilesets;//tile集合队列
        List<MapLayer> mapLayers;//地图层级队列
        static int mapWidth;
        static int mapHeight;
        #endregion
        #region Property Region
        public static int WidthInPixels
        {
            get { return mapWidth * Engine.TileWidth; }
        }
        public static int HeightInPixels
        {
            get { return mapHeight * Engine.TileHeight; }
        }
        #endregion
        #region Constructor Region//构造函数
        public TileMap(List<Tileset> tilesets, List<MapLayer> layers)
        {
            this.tilesets = tilesets;
            this.mapLayers = layers;
            mapWidth = mapLayers[0].Width;
            mapHeight = mapLayers[0].Height;
            for (int i = 1; i < layers.Count; i++)
            {
                //检测到各个层之间的宽度，高度不一致，抛出异常
                if (mapWidth != mapLayers[i].Width || mapHeight != mapLayers[i].Height)
                    throw new Exception("Map layer size exception");
            }
        }
        public TileMap(Tileset tileset, MapLayer layer)
        {
            tilesets = new List<Tileset>();
            tilesets.Add(tileset);
            mapLayers = new List<MapLayer>();
            mapLayers.Add(layer);
            mapWidth = mapLayers[0].Width;//初始化
            mapHeight = mapLayers[0].Height;
        }
        #endregion
        #region Method Region
        //地图绘制
        public void Draw(SpriteBatch spriteBatch, Camera camera)
        {
            Rectangle destination = new Rectangle(0, 0, Engine.TileWidth, Engine.TileHeight);
            Tile tile;
            foreach (MapLayer layer in mapLayers)
            {
                for (int y = 0; y < layer.Height; y++)
                {
                    destination.Y = y * Engine.TileHeight - (int)camera.Position.Y;//获得绘制tile的Y坐标
                    for (int x = 0; x < layer.Width; x++)
                    {
                        tile = layer.GetTile(x, y);
                        if (tile.TileIndex == -1 || tile.Tileset == -1)
                            continue;
                        destination.X = x * Engine.TileWidth - (int)camera.Position.X;//获得绘制tile的X坐标
                        spriteBatch.Draw(
                        tilesets[tile.Tileset].Texture,
                        destination,
                        tilesets[tile.Tileset].SourceRectangles[tile.TileIndex],
                       Color.White);
                    }
                }
            }
        }
        #endregion
        public void AddLayer(MapLayer layer)
        {
            //首先判断每层的宽度，高度是否相同
            if (layer.Width != mapWidth && layer.Height != mapHeight)
                throw new Exception("Map layer size exception");
            mapLayers.Add(layer);
        }
    }
}